using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Button))]
public class UIDoubleStateButton : MonoBehaviour
{
	public Image targetImage;

	public Sprite onActive;

	public Sprite onInactive;

	[Space(20f)]
	public DoubleStateButtonEvent onStateChanged = new DoubleStateButtonEvent();

	private bool state;

	public void SetState(bool state, bool raiseEvent = false)
	{
		this.state = state;
		SetGraphics();
		if (raiseEvent)
		{
			onStateChanged.Invoke(state);
		}
	}

	private void SetGraphics()
	{
		if (state)
		{
			targetImage.sprite = onActive;
		}
		else
		{
			targetImage.sprite = onInactive;
		}
	}

	public void OnStateChanged()
	{
		SetState(!state, raiseEvent: true);
	}

	private void Reset()
	{
		Button component = GetComponent<Button>();
		component.transition = Selectable.Transition.None;
		Image component2 = GetComponent<Image>();
		if (component2 != null)
		{
			targetImage = component2;
			onActive = component2.sprite;
		}
	}
}
